<template>
	<view class="page">
		<flowbefore :beflows="beflows" :status="status" :isBtnClick="isBtnClicks" @save="save" @submit="submit" :statusType="statusType">
			<template>
				<view class="padding-bottom-100">
					<form class="flowform">
						<view class="formlist inputtext">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>流程标题</label><text class="required">*</text></view>
								<view class="controlvalue">
									<input type="text" v-model="archival.flowTitle" name="flowTitle" :disabled="disabled" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext ">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>单号编号</label></view>
								<view class="controlvalue">
									<input type="text" v-model="archival.billNo" name="billNo" disabled="true" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext" @tap="flowUrgent">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>紧急程度</label><text class="required">*</text></view>
								<view class="controlvalue">
									<input type="text" v-model="archival.flowUrgent" name="flowUrgent" placeholder="选择" disabled="true" />
								</view>
								<view class="advanceicon iconfont icon-xiangyou"></view>
							</view>
						</view>
						<view class="formlist inputtext">
							<view class="formgroup">
								<view class="labletitle"><label>申请人员</label></view>
								<view class="controlvalue">
									<input type="text" v-model="archival.applyUser" name="applyUser" disabled="true" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext margin-top-20">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>借阅部门</label></view>
								<view class="controlvalue">
									<input type="text" v-model="archival.borrowingDepartment" name="borrowingDepartment" placeholder="输入部门"
									 :disabled="disabled" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext ">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>档案名称</label><text class="required">*</text></view>
								<view class="controlvalue">
									<input type="text" v-model="archival.archivesName" name="archivesName" placeholder="输入档案名称" :disabled="disabled" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>档案编号</label><text class="required">*</text></view>
								<view class="controlvalue">
									<input type="text" v-model="archival.archivesId" name="archivesId" placeholder="输入档案编号" :disabled="disabled" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext" @tap="borrowingDate">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>借阅时间</label><text class="required">*</text></view>
								<view class="controlvalue">
									<input type="text" v-model="archival.borrowingDate" name="borrowingDate" placeholder="选择" disabled="true" />
								</view>
								<view class="advanceicon iconfont icon-xiangyou"></view>
							</view>
						</view>
						<view class="formlist inputtext" @tap="returnDate">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>归还时间</label><text class="required">*</text></view>
								<view class="controlvalue">
									<input type="text" v-model="archival.returnDate" name="returnDate" placeholder="选择" disabled="true" />
								</view>
								<view class="advanceicon iconfont icon-xiangyou"></view>
							</view>
						</view>
						<view class="formlist inputtext" @tap="archivalAttributes">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>档案属性</label><text class="required">*</text></view>
								<view class="controlvalue">
									<input type="text" v-model="archival.archivalAttributes" name="archivalAttributes" disabled="true"
									 placeholder="选择" />
								</view>
								<view class="advanceicon iconfont icon-xiangyou"></view>
							</view>
						</view>
						<view class="formlist inputtext" @tap="borrowMode">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>借阅方式</label><text class="required">*</text></view>
								<view class="controlvalue">
									<input type="text" v-model="archival.borrowMode" name="borrowMode" placeholder="选择" disabled="true" />
								</view>
								<view class="advanceicon iconfont icon-xiangyou"></view>
							</view>
						</view>
						<view class="formlist">
							<view class="formgroup  padding-17">
								<view class="labletitletar"><label>申请原因</label></view>
								<view class="controlvalue  uni-textarea">
									<textarea v-model="archival.applyReason" name="applyReason" maxlength="100" placeholder="输入" :disabled="disabled" />
									</view>
							</view>
						</view>
						<view class="height-100"></view>
					</form>
				</view>
			</template>
		</flowbefore>
		<w-picker :mode="mode" :pickerList="pickerList" ref="picker" :isSearchShow="isSearchShow"></w-picker>
	</view>
</template>

<script>
	import jnpf from '@/common/js/jnpf.js'
	import helper from '@/common/js/helper.js'
	import graceChecker from '@/common/js/graceChecker.js'
	import wPicker from '@/components/w-picker/w-picker.vue'
	import flowbefore from '@/components/work/flow-before.vue'
	let formRule=[
			{name:"flowTitle",checkType:"notnull",checkRule:"",errorMsg:"输入标题"},
			{name:"flowUrgent", checkType : "notnull", checkRule:"",  errorMsg:"选择紧急程度"},
			{name:"borrowMode", checkType : "notnull", checkRule:"",  errorMsg:"选择借阅方式"},
			{name:"archivalAttributes", checkType : "notnull", checkRule:"",  errorMsg:"选择档案属性"},
			{name:"archivesName",checkType:"notnull",checkRule:"",errorMsg:"输入档案名称"},
			{name:"archivesId", checkType : "notnull", checkRule:"",  errorMsg:"输入档案编号"},
			{name:"borrowingDate", checkType : "notnull", checkRule:"",  errorMsg:"选择借阅时间"},
			{name:"returnDate", checkType : "notnull", checkRule:"",  errorMsg:"选择归还时间"},
		];
	export default {
		data() {
			return {
				mode:'',//底部弹窗分类
				isSearchShow:false,
				disabled:false,//启用
				status:'',//流程状态
				beflows:{
					operstate:"",
					processId:''
				},
				archival:{
					billNo:'',
					flowUrgent:'',
					flowTitle:jnpf.userInfo().realName+'的档案借阅申请',
					applyUser:jnpf.userInfo().realName+"/"+jnpf.userInfo().account,
					borrowingDepartment:'',
					archivesName:'',
					archivesId:'',
					borrowingDate:'',
					returnDate:'',
					archivalAttributes:'',
					borrowMode:'',
					applyReason:''
				},
				isBtnClicks:false,
				pickerList:[],
				urgentList:[{
					label: "普通",
					value:"1"
				},
				{
					label: "重要",
					value:"2"
				},
				{
					label: "紧急",
					value:"3"
				}],
				archivalAttributesList:[{
					label: "原件",
					value:"0"
				},
				{
					label: "复印件",
					value:"1",
				},
				{
					label: "其他",
					value:"2",
				}],
				borrowModeList:[{
					label: "查阅",
					value:"0"
				},
				{
					label: "复印",
					value:"1",
				},
				{
					label: "外借",
					value:"2",
				},
				{
					label: "其他",
					value:"3",
				}],
				statusType:''
			}
		},
		onLoad(e) {
			/* 工作区进入状态 */
			let statusType = uni.getStorageSync("type")
			this.statusType = statusType
			/* 工作区进入状态 End*/
			let flowInfo= JSON.parse(e.flowInfo);
			this.beflows.operstate=flowInfo.operstate;
			if(flowInfo.operstate =='add'){
				this.archival.flowId=flowInfo.id;
				this.status = 0;
				this.archival.flowUrgent = this.getSpinner(this.urgentList,'1');
				helper.ajax({
					url: '/api/System/BillRule/BillNumber/WF_ArchivalBorrowNo',
					success: res => {
						this.archival.billNo=res.data.data;
					}
				});
			}
			if(flowInfo.operstate=='info'){
				this.disabled=flowInfo.disabled;
				this.beflows.processId=flowInfo.id;
				this.status=flowInfo.status;
				helper.ajax({
					url: '/api/WorkFlowForm/ArchivalBorrow/' + flowInfo.id,
					success: res => {
						let _archival=res.data.data;
						this.archival = _archival;
						this.archival.flowUrgent = this.getSpinner(this.urgentList,_archival.flowUrgent);
						this.archival.borrowingDate = helper.toDate(_archival.borrowingDate, "yyyy-MM-dd HH:mm");
						this.archival.returnDate = helper.toDate(_archival.returnDate, "yyyy-MM-dd HH:mm");
					}
				});
			}
		},
		methods: {
			setSpinner: function(list, label) {
				let spinner
				for (var i = 0; i < list.length; i++) {
					if (list[i].label == label) {
						spinner = list[i].value;
						break;
					}
				}
				return spinner
			},
			getSpinner: function(list, value) {
				let spinner
				for (var i = 0; i < list.length; i++) {
					if (list[i].value == value) {
						
						spinner = list[i].label;
						break;
					}
				}
				return spinner
			},
			
			flowUrgent(e) {
				if(!this.disabled){
					this.pickerList = this.urgentList;
					this.mode="selector";
					this.$refs.picker.show();
					this.$eventHub.$off('popupmodality');
					this.$eventHub.$on('popupmodality', (data) => {
						this.archival.flowUrgent = data.result;
						this.$eventHub.$off('popupmodality');
					});
				}
			},
			borrowingDate(e){
				if(!this.disabled){
					this.mode="dateTime";
					this.$refs.picker.show();
					this.$eventHub.$off('popupmodality');
					this.$eventHub.$on('popupmodality', (data) => {
						this.archival.borrowingDate = data.result;
						this.$eventHub.$off('popupmodality');
					});
				}
			},
			returnDate(e){
				if(!this.disabled){
					this.mode="dateTime";
					this.$refs.picker.show();
					this.$eventHub.$off('popupmodality');
					this.$eventHub.$on('popupmodality', (data) => {
						this.archival.returnDate = data.result;
						this.$eventHub.$off('popupmodality');
					});
				}
			},
			archivalAttributes(e){
				if(!this.disabled){
					this.pickerList=this.archivalAttributesList;
					this.mode="selector";
					this.$refs.picker.show();
					this.$eventHub.$off('popupmodality');
					this.$eventHub.$on('popupmodality', (data) => {
						this.archival.archivalAttributes = data.result;
						this.$eventHub.$off('popupmodality');
					});
				}
			},
			borrowMode(e){
				if(!this.disabled){
					this.pickerList=this.borrowModeList;
					this.mode="selector";
					this.$refs.picker.show();
					this.$eventHub.$off('popupmodality');
					this.$eventHub.$on('popupmodality', (data) => {
						this.archival.borrowMode = data.result;
						this.$eventHub.$off('popupmodality');
					});
				}
			},
			submit(e){
				let formData=this.archival;
				if (!graceChecker.check(formData,formRule)) {
					helper.msg(graceChecker.error);
					return
				}
				this.isBtnClicks=true;
				formData.flowUrgent = this.setSpinner(this.urgentList,formData.flowUrgent);
				formData.status = 0;
				let method = "POST";
				let url = "/api/WorkFlowForm/ArchivalBorrow";
				if(formData.id){
					method = "PUT";
					url=url+"/"+formData.id
				}
				formData.returnDate = Date.parse(formData.returnDate);
				formData.borrowingDate = Date.parse(formData.borrowingDate);
				helper.ajax({
					url: url,
					method:method,
					contentType: "application/json",
					data:formData,
					success: res => {
						helper.msg(res.data.msg, 'success');
						if (this.beflows.operstate=='add') {
							uni.redirectTo({
								url: '../../operate/started/started',
								success: res => {},
								fail: () => {},
								complete: () => {}
							});
						}else{
							uni.navigateBack();
						}
						this.isBtnClicks=false
					}
				});
			},
			save(e){
				let formData = this.archival;
				// console.log(this.archival)
				if (!graceChecker.check(formData,formRule)) {
					helper.msg(graceChecker.error);
					return
				}
				this.isBtnClicks=true;
				formData.flowUrgent = this.setSpinner(this.urgentList,formData.flowUrgent);
				formData.status = 1;
				let method = "POST";
				let url = "/api/WorkFlowForm/ArchivalBorrow";
				if(formData.id){
					method = "PUT";
					url=url+"/"+formData.id
				}
				formData.returnDate = Date.parse(formData.returnDate);
				formData.entryList = this.detailproList;
				formData.borrowingDate = Date.parse(formData.borrowingDate);
				helper.ajax({
					url: url,
					method:method,
					contentType: "application/json",
					data:formData,
					success: res => {
						helper.msg(res.data.msg, 'success');
						if (this.beflows.operstate=='add') {						
							uni.redirectTo({
								url: '../../operate/started/started',
								success: res => {},
								fail: () => {},
								complete: () => {}
							});
						}else{
							uni.navigateBack();
						}
						this.isBtnClicks=false
					}
				});
			},
		},
		components: {
			wPicker,
			flowbefore,
		}
	}
</script>

<style>
	.required{
		color: red;
	}
</style>
